<template>
	<view class="content">
		<view>
			<view class="ipts" v-if="read_only">
				<view class="record">
					<view class="">
						<image src="/static/jilu.png" mode=""></image>
						<span>用户信息</span>
					</view>
				</view>
				<!-- 户号 -->
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>档案号</span>
					</view>
					<span class="place-class">{{list.user_text.meter_num}}</span>
				</view>

				<!-- 户主姓名 -->
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>户主姓名</span>
					</view>
					<span class="place-class">{{list.user_text.realname}}</span>
				</view>

				<!-- 身份证号 -->
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>身份证号</span>
					</view>
					<span class="place-class">{{list.user_text.ID_num}}</span>
				</view>

				<!-- 联系方式 -->
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>联系方式</span>
					</view>
					<view class="" style="display: flex;align-items: center;">
						<image src="/static/pagesB/tel.png" mode=""></image>
						<span class="place-class">{{list.user_text.mobile}}</span>
					</view>
				</view>

				<!-- 地址 -->
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>地址</span>
					</view>
					<span class="place-class">{{list.user_text_adress}}</span>
				</view>
			</view>
			<!-- <view class="info-item" v-else>
				<view class="info-item-left">
					<view class="dot"></view>
					<span>身份证号</span>
				</view>
				<input class="item-ipt" type="idcard" v-model="copyData.ID_num" placeholder="请输入身份证号" />
			</view> -->
			<view class="info-item" v-else>
				<view class="info-item-left">
					<view class="dot"></view>
					<span>输入档案号</span>
				</view>
				<input class="item-ipt" type="text" v-model="copyData.archives_num" placeholder="请输入档案号" />
			</view>
			<!-- 第二块 -->
			<view class="ipts">
				<!-- 抄表日期 -->
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>抄表日期</span>
					</view>
					<input @tap="isTime" class="item-ipt" disabled type="text" v-model="copyData.time"
						placeholder="请上传抄表日期" />
				</view>

				<!-- 客户编号 -->
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>客户编号</span>
					</view>
					<input class="item-ipt" type="text" :disabled="read_only" v-model="copyData.member_id"
						placeholder="请输入客户编号" />
				</view>

				<!-- 单位名称 -->
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>单位名称</span>
					</view>
					<input class="item-ipt" type="text" :disabled="read_only" v-model="copyData.unit_name"
						placeholder="请输入单位名称" />
				</view>

				<!-- 地址 -->
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>地址</span>
					</view>
					<view class="" style="display: flex;align-items: center;">
						<input class="item-ipt" type="text" :disabled="read_only" v-model="copyData.address"
							placeholder="请输入请输入地址" />
					</view>
				</view>

				<!-- 流量编号 -->
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>流量编号</span>
					</view>
					<input class="item-ipt" type="text" :disabled="read_only" v-model="copyData.flow_number"
						placeholder="请输入流量编号" />
				</view>

				<!-- 添加流量计照片 -->
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>添加流量计照片</span>
					</view>
					<view class="addform-items">
						<template v-for="i,index in copyData.flow_images">
							<view class="" style="position: relative;">
								<image class="form-img" :src="i" @tap="seeimg('flow_images',index)"></image>
								<image v-if="!read_only" class="delet" src="/static/delet.png" mode=""
									@tap="deletImg('flow_images',index)"></image>
							</view>
						</template>

						<view class="add" @tap="choosefrom('flow_images')" v-if="!read_only">
							<u-icon name="camera" color="#999999" size="28"></u-icon>
						</view>
					</view>
				</view>

				<!-- 修正仪编号 -->
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>修正仪编号</span>
					</view>
					<input class="item-ipt" type="text" :disabled="read_only" v-model="copyData.amendment_number"
						placeholder="请输入修正仪编号" />
				</view>

				<!-- 添加修正仪照片 -->
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>添加修正仪照片</span>
					</view>
					<view class="addform-items">
						<template v-for="i,index in copyData.amendment_images">
							<view class="" style="position: relative;">
								<image class="form-img" :src="i" @tap="seeimg('amendment_images',index)"></image>
								<image class="delet" v-if="!read_only" src="/static/delet.png" mode=""
									@tap="deletImg('amendment_images',index)"></image>
							</view>
						</template>

						<view class="add" @tap="choosefrom('amendment_images')" v-if="!read_only">
							<u-icon name="camera" color="#999999" size="28"></u-icon>
						</view>
					</view>
				</view>

				<!-- 上期读数 -->
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>上期读数</span>
					</view>
					<input class="item-ipt" type="digit" :disabled="read_only" v-model="copyData.prior_reading"
						placeholder="请输入上期读数" />
				</view>

				<!-- 本期读数 -->
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>本期读数</span>
					</view>
					<input class="item-ipt" type="digit" :disabled="read_only" v-model="copyData.current_reading"
						placeholder="请输入本期读数" />
				</view>

				<!-- 本期实用数 -->
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>本期实用数</span>
					</view>
					<input class="item-ipt" disabled v-model="copyData.current_employ_number" placeholder="本期实用数" />
				</view>

				<!-- 本期实用天数 -->
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>本期实用天数</span>
					</view>
					<input class="item-ipt" type="number" :disabled="read_only" v-model="copyData.current_employ_days"
						placeholder="请输入本期实用天数" />
				</view>

				<!-- 本期平均日用气量 -->
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>本期平均日用气量</span>
					</view>
					<input class="item-ipt" disabled v-model="copyData.current_average" placeholder="本期平均日用气量" />
				</view>

				<!-- 本期单价 -->
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>本期单价</span>
					</view>
					<input class="item-ipt" type="digit" :disabled="read_only" v-model="copyData.current_unit_price"
						placeholder="请输入本期单价" />
				</view>

				<!-- 本期气费 -->
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>本期气费</span>
					</view>
					<input class="item-ipt" disabled v-model="copyData.current_cost.toFixed(2)" placeholder="本期气费" />
				</view>

			</view>

			<!-- 图片资料 -->
			<view class="ipts">
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>资料照片</span>
					</view>
					<view class="addform-items">
						<template v-for="i,index in copyData.images">
							<view class="" style="position: relative;">
								<image class="form-img" :src="i" @tap="seeimg('images',index)"></image>
								<image v-if="!read_only" class="delet" src="/static/delet.png" mode=""
									@tap="deletImg('images',index)"></image>
							</view>
						</template>
						<view class="add" v-if="!read_only" @tap="choosefrom('images')">
							<u-icon name="camera" color="#999999" size="28"></u-icon>
						</view>
					</view>
				</view>
			</view>
			<!-- 第四块 -->
			<view class="ipts" v-if="false">
				<!-- 平均气量 -->
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>平均气量</span>
					</view>
					<input class="item-ipt" type="text" :disabled="read_only" v-model="copyData.avg"
						placeholder="请输入平均气量" />
				</view>

				<!-- 总费用 -->
				<view class="info-item">
					<view class="info-item-left">
						<view class="dot"></view>
						<span>总费用</span>
					</view>
					<input class="item-ipt" type="text" :disabled="read_only" v-model="copyData.sum_value"
						placeholder="请输入总费用" />
				</view>
			</view>
		</view>
		<view class="btns" v-if="!read_only">
			<view class="btn1 consel" @click="flag= true,searchIpt = ''">取消</view>
			<view class="btn1 save" @click="userPost">保存</view>
		</view>
		<u-picker v-model="show" mode="time" :params="params" @confirm="confirm"></u-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				read_only: false,
				_header:'',
				list: '',
				copyData: {
					ID_num: '',
					archives_num: '',
					time: '',
					member_id: '',
					unit_name: '',
					address: '',
					flow_number: '',
					flow_images: [],
					amendment_number: '',
					amendment_images: [],
					prior_reading: '',
					current_reading: '',
					current_employ_number: '',
					current_employ_days: '',
					current_average: '',
					current_unit_price: '',
					current_unit_price_number: '',
					images: [],
					current_cost: '',
					avg: '',
					sum_value: ''
				},
				show: false,
				params: {
					year: true,
					month: true,
					day: true,
					hour: true,
					minute: true,
					second: true
				},
			}
		},
		onShow() {},
		onLoad(e) {
			 this._header= this._header
			if (e.id) {
				
				this.read_only = true
				this.initData(e.id)
			}
		},
		computed: {
			//本期实用数
			currentNum() {
				let num = 0
				if (this.copyData.current_reading && this.copyData.prior_reading) {
					this.copyData.current_reading = Number(this.copyData.current_reading)
					this.copyData.prior_reading = Number(this.copyData.prior_reading)
					if (this.copyData.current_reading > this.copyData.prior_reading) {
						num = this.copyData.current_reading - this.copyData.prior_reading
						this.copyData.current_employ_number = num
					} else {
						this.copyData.current_employ_number = ''
					}
				} else {
					this.copyData.current_employ_number = ''
				}
				return num
			},
			// 本期平均日用气量
			meanNum() {
				let num = 0

				if (this.copyData.current_employ_number && this.copyData.current_employ_days) {
					this.copyData.current_employ_number = Number(this.copyData.current_employ_number)
					this.copyData.current_employ_days = Number(this.copyData.current_employ_days)
					num = this.copyData.current_employ_number / this.copyData.current_employ_days
					num = num.toFixed(2)
					this.copyData.current_average = num
				}
				return num
			},
			price() {
				let num = 0
				if (this.copyData.current_unit_price && this.copyData.current_employ_number) {
					this.copyData.current_unit_price = Number(this.copyData.current_unit_price)
					this.copyData.current_employ_number = Number(this.copyData.current_employ_number)
					num = this.copyData.current_employ_number * this.copyData.current_unit_price
					this.copyData.current_cost = num
					return num
				} else {
					this.copyData.current_cost = ''
				}
			}
		},
		methods: {
			cloneArr(obj, deep) {
				// 判断如果传入对象是一个数组
				if (Array.isArray(obj)) {
					if (deep) { // 开启深度克隆
						var newArr = []; // 创建一个新数组
						// 循环数组
						for (var i = 0; i < obj.length; i++) {
							newArr.push(this.cloneArr(obj[i], deep)); // 如果数组里还是数组 那么再次调用这个函数进行判断
						}
						return newArr;
					} else { // 没有开启深度克隆直接复制一个数组返回
						return obj.slice()
					}
				} else if (typeof obj === 'object') {
					// 克隆一个对象
					var newObj = {};
					for (var prop in obj) {
						// 是否开启了深度克隆
						if (deep) {
							// 深度克隆
							newObj[prop] = this.cloneArr(obj[prop], deep); // 如果对象里还有对象 那么再次调用这个函数进行判断
						} else {
							newObj[prop] = obj[prop]; //复制属性
						}
					}
					return newObj;
				} else {
					// 以上都不是 或者 如果是一个字符串、数字 ，直接返回该数据
					return obj
				}
			},
			initData(id) {
				var _this = this
				uni.request({
					url: _this.$url + 'gas.copy_gas_record/details',
					method: 'GET',
					data: {
						id: id
					},
					success: (res) => {
						_this.list = res.data.data
						_this.copyData = res.data.data
						this.copyData.flow_images = this.copyData.flow_images.split(',')
						this.copyData.amendment_images = this.copyData.amendment_images.split(',')
						this.copyData.images = this.copyData.images.split(',')
					}
				})
			},
			userPost() {
				var _this = this
				const copyData = _this.cloneArr(_this.copyData, true)
				copyData.flow_images = copyData.flow_images.toString()
				copyData.amendment_images = copyData.amendment_images.toString()
				copyData.images = copyData.images.toString()
 
 
				console.log(_this._header)	
 
				uni.request({
					url: _this.$url + 'gas.copy_gas_record/add',
					method: 'POST',
					header:{
						'token':uni.getStorageSync('token')
					},
					data: copyData,
					success: (res) => {
						_this.$showToast(res.data.msg)
						copyData.flow_images = copyData.flow_images.split(',')
						copyData.amendment_images = copyData.amendment_images.split(',')
						copyData.images = copyData.images.split(',')
						if (res.data.code == 1) {
							setTimeout(() => {
								uni.navigateBack({
									delta: 1
								})
							}, 1000)
						}
					}
				})
			},

			isTime() {
				if (!this.read_only) {
					this.show = true
				}
			},
			confirm(e) {
				console.log(e);
				this.copyData.time = e.year + '-' + e.month + '-' + e.day + ' ' + e.hour + ':' + e.minute + ':' + e.second
			},
			// 选择图片
			choosefrom(img) {
				if (this.copyData[img].length < 3) {
					var _this = this
					uni.chooseImage({
						count: 1,
						success: (res) => {
							uni.uploadFile({
								url: _this.$api + 'common/upload',
								filePath: res.tempFilePaths[0],
								name: 'file',
								header: {
									token: uni.getStorageSync('token')
								},
								success: (uploadFileRes) => {
									if (JSON.parse(uploadFileRes.data).code == 1) {
										_this.copyData[img].push(JSON.parse(uploadFileRes.data)
											.data.intact_url)
									}
								},
								fail: (err) => {
									console.log(err);
								}
							})

						}
					})
				} else {
					uni.showToast({
						icon: 'none',
						title: '上传已达到限制'
					})
				}
			},
			// 删除图片
			deletImg(img, index) {
				this.copyData[img].splice(index, 1)
			},
			// 图片预览
			seeimg(img, index) {
				uni.previewImage({
					urls: this.copyData[img],
					current: index,
					loop: true
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.addform-items {
		display: flex;
		align-items: center;

		.form-img {
			width: 60rpx !important;
			height: 60rpx !important;
			margin-right: 20rpx;
		}

		.delet {
			position: absolute;
			top: -8rpx;
			right: 4rpx;
			width: 28rpx;
			height: 28rpx;
			z-index: 10;
		}

		.add {
			width: 60rpx;
			height: 60rpx;
			border: 1rpx solid #DDDDDD;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}

	.content {
		padding-bottom: 100rpx;

		.ipts {
			background-color: #fff;
			margin-bottom: 20rpx;

			.liaison {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 0rpx 32rpx;
				height: 100rpx;
				align-items: center;

				.left {
					display: flex;
					align-items: center;

					.shuxian {
						width: 8rpx;
						height: 28rpx;
						background: #003690;
						opacity: 1;
						border-radius: 200rpx;
						margin-right: 10rpx;
					}

					.name {
						font-size: 28rpx;
						font-family: Source Han Sans SC;
						font-weight: 500;
						color: #333333;
						margin-right: 10rpx;
					}

					.time {
						font-size: 24rpx;
						font-family: Source Han Sans SC;
						font-weight: 400;
						color: #777777;
					}
				}

				.right {
					display: flex;
					align-items: center;

					image {
						width: 32rpx;
						height: 32rpx;
						margin-right: 10rpx;
					}

					.phone {
						font-size: 24rpx;
						font-family: Source Han Sans SC;
						font-weight: 400;
						color: #555555;
					}
				}
			}
		}

		.info-item {
			box-sizing: border-box;
			display: flex;
			align-items: center;
			justify-content: space-between;
			// height: 72rpx;
			width: 100%;
			height: 100rpx;
			padding: 0rpx 32rpx;
			align-items: center;
			background-color: #fff;
			font-size: 24rpx;
			color: #333;
			border-bottom: 1rpx solid #DDDDDD;

			image {
				width: 32rpx;
				height: 32rpx;
			}

			input {
				flex: 1;
				text-align: right;
			}

			.dot {
				width: 8rpx;
				height: 8rpx;
				background-color: #003690;
				border-radius: 50%;
				margin-right: 10rpx;
			}

			.danger-img {
				position: relative;

				.updeletes {
					position: absolute;
					top: -20rpx;
					right: -20rpx;
					width: 40rpx;
					height: 40rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					z-index: 88;

					image {
						width: 100%;
						height: 100%;
					}
				}
			}

			.danger-img-box {
				display: flex;

				.danger-img {
					width: 75rpx;
					height: 75rpx;
					// border: 2rpx dashed #eee;
					margin-right: 10rpx;

					image {
						width: 100%;
						height: 100%;
					}
				}
			}

			.danger-img-box2 {
				display: flex;

				.danger-img {
					width: 75rpx;
					height: 75rpx;
					// border: 2rpx dashed #eee;
					margin-right: 10rpx;

					image {
						width: 100%;
						height: 100%;
						opacity: 1;
					}
				}
			}

			.info-item-left {
				display: flex;
				align-items: center;
				font-size: 28rpx;
			}
		}

		.hengxian {
			width: 700rpx;
			height: 2rpx;
			background-color: #eee;
			margin: 20rpx auto;
		}

		.place-class {
			font-size: 28rpx;
			color: #999;
		}

		.btn {
			width: 646rpx;
			height: 80rpx;
			margin: 74rpx auto;
			text-align: center;
			line-height: 80rpx;
			border-radius: 40rpx;
			background-image: linear-gradient(to bottom, #2366D5, #001B48);
			font-size: 32rpx;
			color: #E5EFFE;
		}

		.btns {
			position: fixed;
			bottom: 0;
			display: flex;
			width: 100%;
			z-index: 333;

			.btn1 {
				width: 50%;
				height: 96rpx;
				line-height: 96rpx;
				text-align: center;
				font-size: 24rpx;
			}

			.consel {
				background-color: #e9e9e9;
				color: #777;
			}

			.save {
				background-color: #003690;
				color: #fff;
			}
		}

		.btn:active {
			background: rgba(0, 0, 0, 0.4);
		}

		.zhanwei {
			height: 250rpx;
		}

		.record {
			display: flex;
			align-items: center;
			justify-content: space-between;
			color: #003690;
			font-size: 32rpx;
			// padding-top: 20rpx;
			padding: 0rpx 0rpx;
			height: 100rpx;
			align-items: center;
			border-bottom: 1rpx solid #DDDDDD;


			.right {
				width: 394rpx;
				height: 52rpx;
				background: #F2F2F2;
				opacity: 1;
				border-radius: 50rpx;
				padding: 0 10rpx 0 15rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
			}

			image {
				width: 32rpx;
				height: 32rpx;
				margin: 0 20rpx 0 30rpx;
			}

			span {
				font-weight: 700;
			}
		}

		.record-item {
			display: flex;
			justify-content: space-between;
			padding: 10rpx 30rpx;
			border-bottom: 2rpx solid #eee;

			.record-item-left {
				display: flex;
				align-items: center;

				.shu {
					width: 8rpx;
					height: 28rpx;
					background-color: #003690;
					border-radius: 8rpx;
				}

				.title {
					font-size: 32rpx;
					color: #333;
					font-weight: 500;
					margin: 0 10rpx;
				}

				.time {
					font-size: 24rpx;
					color: #777;
				}
			}

			.record-item-right {
				display: flex;
				align-items: center;

				.dianhua {
					width: 28rpx;
					height: 28rpx;
				}

				.zhankai {
					width: 36rpx;
					height: 36rpx;
				}

				.title {
					font-size: 32rpx;
					color: #555;
					font-weight: 500;
					margin: 0 10rpx;
				}
			}
		}

		.supplement {
			margin: 20rpx 24rpx;
			background-color: #fff;
			position: relative;
			overflow: hidden;

			.explain {
				.textArea {
					min-height: 142rpx;
					padding: 14rpx 30rpx;
					font-size: 32rpx;
					text-align: left;
					color: rgba(187, 187, 187, 0.84);
				}
			}

			.suppBottom {
				.danger-img {
					position: relative;
					margin-left: 50rpx;
					margin-bottom: 0rpx;
					width: 68rpx;
					height: 68rpx;
					float: left;

					image {
						width: 36rpx;
						height: 36rpx;
						position: absolute;
						left: 50%;
						top: 50%;
						transform: translate(-50%, -50%);
					}
				}

				.num {
					float: right;
					margin-right: 26rpx;
				}
			}
		}
	}

	// 	// 搜索框
	.header1 {
		display: flex;
		justify-content: space-between;
		align-items: center;
		box-sizing: border-box;
		height: 80rpx;
		width: 100%;
		background-color: #003690;
		padding: 0 30rpx;

		.ipt-box {
			display: flex;
			justify-content: space-between;
			align-items: center;
			box-sizing: border-box;
			padding: 0 20rpx;
			width: 480rpx;
			height: 52rpx;
			background-color: #fff;
			border-radius: 24rpx;

			image {
				width: 32rpx;
				height: 32rpx;
			}
		}

		.header1-right {
			display: flex;
			align-items: center;
			font-size: 28rpx;
			color: #E2ECFB;

			image {
				width: 36rpx;
				height: 36rpx;
			}
		}
	}
</style>
